package com.scau.os.controller;

import com.scau.os.MemorySys.*;
import com.scau.os.extension.EventSystem;
import com.scau.os.extension.MyDelegate;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;

import java.net.URL;
import java.util.*;
import java.util.List;

import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class memoryManagerController implements Initializable {
    Memory memory = UseMemory.memory;

    LinkedList<Hole> hole_list = new LinkedList<>();
    @FXML
    HBox ramView;
    @FXML
    Button button2 ;
    public Canvas canvas = new Canvas(600,250);
    protected void updateMemory() {
        GraphicsContext gs = canvas.getGraphicsContext2D();
        //颜色，维护colorList
        ArrayList<Color> colorList = new ArrayList<>(30);
        for (int i = 0 ;i<30;i++){
            double red = Math.random()*100;
            double blue = Math.random()*100;
            double green = Math.random()*100;
            double opacityRandom = Math.random()*100;
            Color c = new Color(red/100,blue/100,green/100,opacityRandom/100);
            if (c != Color.WHITE&& c !=Color.LIGHTBLUE){

                colorList.add(c);
            }
        }
        //每隔块的大小信息
        hole_list = memory.getHoles();
        //遍历这个块的链表信息，将内存块画到gs当中
        paint(gs,hole_list,colorList);

    }

    public static void paint(GraphicsContext gs, List<Hole> list, ArrayList<Color> colorArrayList){

        int m = 0,size_new,width_old = 0;
        int i = list.size();
        int width = 25;
        gs.setFill(Color.LIGHTBLUE);
        gs.fillRect(10,10,532,100);
        gs.setFill(Color.WHITE);
        gs.fillRect(20,20,512,80);

        for (Hole hole:list){
            System.out.println(hole);
            gs.setFill(colorArrayList.get(m));
            size_new = hole.getSize();
            gs.fillRect(width_old+20, 20, size_new , 80);
            gs.setFill(Color.BLACK);
            if (hole.isFree()){
                gs.setFont(new Font(10));
                gs.fillText("空闲:"+String.valueOf(hole.getSize()), width_old+(size_new)/2+10, 20 + 20);
            }
            else if(!hole.isFree()){
                gs.setFont(new Font(20));
                gs.fillText(String.valueOf(hole.getSize()), width_old+(size_new)/2+10, 20 + 20);
            }
            width_old += size_new;
            m++;
        }

    }


    private void register(){
        EventSystem.instance.register(new MyDelegate("RefreshMemory") {
            @Override
            public void execute(Object data) {
                updateMemory();
            }
        }).UnRegisterWhenClose(this);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        register();
        updateMemory();
    }
}
